package com.dingding.web.driver.action;

import javax.annotation.Resource;

import com.dingding.common.core.exception.bussiness.BussinessException;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.oss.upload.ImageUpload;
import com.dingding.common.core.util.paravalidate.ValidateUtil;
import com.dingding.facade.mall.status.StatusCode;
import com.dingding.facade.user.common.dto.common.ReqUserLoginByWeb;
import com.dingding.facade.user.common.dto.common.ResUserLoginByWeb;
import com.dingding.facade.user.dto.common.driver.ReqDriverAuthDTO;
import com.dingding.facade.user.dto.common.driver.ReqDriverAuthImageDTO;
import com.dingding.facade.user.dto.common.driver.ReqDriverUserDTO;
import com.dingding.facade.user.dto.common.driver.ReqSyncDriverAuthStateDTO;
import com.dingding.facade.user.dto.common.driver.ResDriverAuthDTO;
import com.dingding.facade.user.dto.common.driver.ResDriverAuthDetailsDTO;
import com.dingding.facade.user.dto.common.driver.ResDriverAuthImageDTO;
import com.dingding.facade.user.dto.common.driver.ResSyncDriverAuthStateDTO;
import com.dingding.facade.user.facade.common.driver.IDriverFacade;
import com.dingding.facade.user.facade.common.driver.IUserLoginFacade;
import com.dingding.facade.user.facade.common.driver.IValidateFacade;
import com.dingding.web.base.action.BaseAction;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 
 * @ClassName: DriverAction 
 * @Description: 司机招募相关的接口
 * @author hufan
 * @date 2016年12月3日 上午10:12:54 
 *
 */
public class DriverAction extends BaseAction implements ModelDriven<ReqDriverAuthDTO>{
	
	private static final long serialVersionUID = -7687474963820510491L;

	private ReqDriverAuthDTO reqDriverAuthDTO = new ReqDriverAuthDTO();
	
	@Resource(name = "driverFacade")
	private IDriverFacade driverFacade;
	
	@Resource(name = "validateFacade")
	private IValidateFacade validateFacade;
	
	@Resource(name = "userLoginByWebFacade")
	private IUserLoginFacade userLoginByWebFacade;
	
	/**
	 * 
	 * @Title: webUserLogin 
	 * @Description: web司机招募登录接口
	 * @param  参数说明 
	 * @author hufan
	 * @return 返回类型 
	 * @throws
	 */
	public String webUserLogin() {
		
		ReqUserLoginByWeb reqUserLoginByWeb = new ReqUserLoginByWeb();
		reqUserLoginByWeb.setTelPhone(getPara("telPhone"));
		reqUserLoginByWeb.setValidateCode(getPara("code"));
		
		ResUserLoginByWeb results = null;
		try {
			results = userLoginByWebFacade.userLoginByWeb(reqUserLoginByWeb);
		} catch (BussinessException e) {//业务逻辑异常
			super.writeResponseJsonp(e.getCode(), e.getErrorMessage());
			return NONE;
		} catch (Exception e) {//服务器级别异常
			log.error(e);
			super.writeResponseJsonp(StatusCode.FAILED, StatusCode.FAILED_MESSAGE);
			return NONE;
		}
		
		super.write(getResJsonp(results));
		return NONE;
	}
	
	/**
	 * 
	 * @Title: uploadDriverAuthImage 
	 * @Description: 上传认证车主图片
	 * @param  参数说明 
	 * @author hufan
	 * @return 返回类型 
	 * @throws
	 */
	public String uploadDriverAuthImage() {

		ReqDriverAuthImageDTO reqDriverAuthImageDTO = new ReqDriverAuthImageDTO(getPara("uploadPictures"),getPara("userId"));

		// 验证参数是否为空
		if (ValidateUtil.validateBean(reqDriverAuthImageDTO, ReqDriverAuthImageDTO.class) == false) {
			super.write(getResJsonp(null));
			return NONE;
		}
		
		ResDriverAuthImageDTO result = null;
		try {
			String imgName = new ImageUpload().uploadWebPicture(reqDriverAuthImageDTO.getUploadPictures());
			if (ToolUtil.isNotBlank(imgName)) {
				result = new ResDriverAuthImageDTO();
				result.setImageName(imgName);
				result.setImageUrl(ToolUtil.imageAddress(imgName));
			} else {
				log.error("上传车辆图片的时候出错！");
				super.writeResponseJsonp(StatusCode.FAILED, StatusCode.FAILED_MESSAGE);
				return NONE;
			}
		}catch (Exception e) {//服务器级别异常
			log.error(e);
			super.writeResponseJsonp(StatusCode.FAILED, StatusCode.FAILED_MESSAGE);
			return NONE;
		}
		
		super.write(getResJsonp(result));
		return NONE;
	}
	
	/**
	 * 
	 * @Title: driverAuth 
	 * @Description: 认证车主
	 * @param  参数说明 
	 * @author hufan
	 * @return 返回类型 
	 * @throws
	 */
	public String driverAuth() {

		// 验证参数是否为空
		if (ValidateUtil.validateBean(reqDriverAuthDTO, ReqDriverAuthDTO.class) == false) {
			super.write(getResJsonp(null));
			return NONE;
		}
		
		ResDriverAuthDTO result = null;

		try {
			result = driverFacade.driverAuth(reqDriverAuthDTO);
		} catch (BussinessException e) {//业务逻辑异常
			super.writeResponseJsonp(e.getCode(), e.getErrorMessage());
			return NONE;
		} catch (Exception e) {//服务器级别异常
			log.error(e);
			super.writeResponseJsonp(StatusCode.FAILED, StatusCode.FAILED_MESSAGE);
			return NONE;
		}
		
		super.write(getResJsonp(result));
		return NONE;
	}
	
	/**
	 * 
	 * @Title: getDriverAuthInfo 
	 * @Description: 获取认证车主信息
	 * @param  参数说明 
	 * @author hufan
	 * @return 返回类型 
	 * @throws
	 */
	public String getDriverAuthInfo() {

		ReqDriverUserDTO reqDriverUserDTO = new ReqDriverUserDTO(getPara("userId"));

		// 验证参数是否为空
		if (ValidateUtil.validateBean(reqDriverUserDTO, ReqDriverUserDTO.class) == false) {
			super.write(getResJsonp(null));
			return NONE;
		}
		
		ResDriverAuthDetailsDTO result = null;
		
		try {
			result = driverFacade.getResDriverAuthDetails(reqDriverUserDTO);
		} catch (BussinessException e) {//业务逻辑异常
			super.writeResponseJsonp(e.getCode(), e.getErrorMessage());
			return NONE;
		} catch (Exception e) {//服务器级别异常
			log.error(e);
			super.writeResponseJsonp(StatusCode.FAILED, StatusCode.FAILED_MESSAGE);
			return NONE;
		}
		
		super.write(getResJsonp(result));
		return NONE;
	}

	
	/**
	 * @Description:获取司机认证状态
	 * @author hufan
	 * @since 2.5.7
	 */
	public String getDriverAuthStatus() {

		ReqSyncDriverAuthStateDTO reqSyncDriverAuthStateDTO = new ReqSyncDriverAuthStateDTO(getPara("userId"));
		ResSyncDriverAuthStateDTO result = null;
		try {
			result = driverFacade.getResSyncDriverAuthState(reqSyncDriverAuthStateDTO);
		} catch (BussinessException e) {//业务逻辑异常
			super.writeResponseJsonp(e.getCode(), e.getErrorMessage());
			return NONE;
		} catch (Exception e) {//服务器级别异常
			log.error(e);
			super.writeResponseJsonp(StatusCode.FAILED, StatusCode.FAILED_MESSAGE);
			return NONE;
		}
		
		super.write(getResJsonp(result));
		return NONE;
	}
	
	
	
	@Override
	public ReqDriverAuthDTO getModel() {
		return reqDriverAuthDTO;
	}
}
